package me.freesoft.distributioin;


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
 
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


/**
 * excel 导出工具类
 * @author ma
 *
 */
public class ExportExcelUtil {

	/**
	 * 工作薄对
	 */
	public XSSFWorkbook wb;   

	/**
	 * 工作表对
	 */
	private XSSFSheet sheet;

	/**
	 * 输出到客户端
	 * @param response      响应对象�?
	 * @param fileName      文件�?
	 * @return
	 * @throws IOException
	 */
	public ExportExcelUtil write(HttpServletResponse response, String fileName) throws IOException {
		response.reset();
		response.setContentType("application/octet-stream; charset=gbk");
		response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
		write(response.getOutputStream());
		return this;
	}

	/**
	 * 输出数据�?
	 * @param os 输出
	 * @return  当前对象
	 * @throws IOException
	 */
	public ExportExcelUtil write(OutputStream os) throws IOException {
	
		
		wb.write(os);
		os.close();
		return this;
	}

	/**
	 * 创建1个excel ,并填充数
	 * @param content       导出的目标数具
	 * @param titleName     excel表头
	 * @return         当前对象
	 */
	public ExportExcelUtil exprotExecl(List<String[]> content, String titleName) {

		// 创建新的 Excel 工作�?
		wb = new XSSFWorkbook();
		
		//  Excel 工作簿中建创1个工作表,其名为缺省 sheet1
		sheet = wb.createSheet();
		 

		// 创建字体
		XSSFFont font = wb.createFont();
		// 把字体颜色设置为红色
		font.setColor(XSSFFont.COLOR_NORMAL);
		// 把字体设置为粗体
		font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
		// 创建格式
		XSSFCellStyle cellStyle = wb.createCellStyle();
		// 把创建的字体付加于格�?
		cellStyle.setFont(font);
		// 设置居中
		cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
		int beginRow = 1;
		for (int i = 0; i < content.size(); i++) {
			// 在工作表中创建一列
			XSSFRow row = sheet.createRow(beginRow++);
			
			
			// 期始列
			int beginCol = 0;
			for (int j = 0; j < content.get(i).length; j++) {
				// 在一行中创建q个表格
				XSSFCell cell = row.createCell((short) beginCol++);
                if(j == 0){
                	cell.setCellStyle(cellStyle);
                }
				if (i == 0) {
					// 把上面的格式付加于一个单元格，第1行字体加粗
					cell.setCellStyle(cellStyle);
				}
				// 设置此单元格中存入的是字符串
				cell.setCellType(XSSFCell.CELL_TYPE_STRING);
                
				// 向此单元格中放入数
				cell.setCellValue(content.get(i)[j]);
			}
		}
		// 合并单元�?
		//Region region = new Region((short) 0, (short) 0, (short) 0, (short) content.get(0).length);
		 
		//sheet.addMergedRegion(region);
	 
		 
		CellRangeAddress region = new CellRangeAddress((short) 0, (short) 0, (short) 0, (short) content.get(0).length);
		sheet.addMergedRegion(region);
	
		//sheet.addMergedRegion(new CellRangeAddress((short)2, (short)0, (short)0, (short)0));
		// 向此单元格中放入row
		XSSFRow row = sheet.createRow(0);
		XSSFCell cell = row.createCell((short) 0);
		// 把上面的格式付加于一个单元格，第1行字体加粗
		cell.setCellStyle(cellStyle);
		cell.setCellStyle(cellStyle);
		cell.setCellValue(titleName);
		// 添加导出时间
		/*CellRangeAddress regionT = new CellRangeAddress((short) (content.size() + 2), (short) (content.get(0).length - 3),
				(short) (content.size() + 2), (short) (content.get(0).length - 1));
		sheet.addMergedRegion(regionT);
		 */
		
		return  this;

	}
}
